//
//  FiltersView.m
//  FengWoFilterTickerDemo
//
//  Created by beebook on 15/10/27.
//  Copyright © 2015年 FengWo. All rights reserved.
//

#import "FiltersView.h"

@implementation FiltersView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor lightGrayColor];
        _scollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scollView.contentSize = frame.size;
        _scollView.pagingEnabled = YES;
        [self addSubview:_scollView];
        
        filterArray = @[@"Original",
//                        @"CIBlendWithAlphaMask",
//                        @"CIColorClamp",
//                        @"CIColorCrossPolynomial",
//                        @"CIColorCubeWithColorSpace",
//                        @"CIColorPolynomial",
//                        @"CIConvolution3X3",
//                        @"CIConvolution5X5",
//                        @"CIConvolution7X7",
//                        @"CIConvolution9Horizontal",
//                        @"CIConvolution9Vertical",
//                        @"CILinearToSRGBToneCurve",
                        @"Saturation",
                        @"Vignette",
                        @"Vintage",
                        @"ToneCurve",
                        @"CIPhotoEffectChrome",
                        @"CIPhotoEffectFade",
                        @"CIPhotoEffectInstant",
                        @"CIPhotoEffectMono",
                        @"CIPhotoEffectNoir",
                        @"CIPhotoEffectProcess",
                        @"CIPhotoEffectTonal",
                        @"CIPhotoEffectTransfer",
                        @"CISRGBToneCurveToLinear",
                        @"CIVignetteEffect",];
        
        filterNameArray = @[@"原图",
//                        @"CIBlendWithAlphaMask",
//                        @"CIColorClamp",
//                        @"CIConvolution3X3",
//                        @"CIConvolution5X5",
//                        @"CIConvolution7X7",
//                        @"CIConvolution9Horizontal",
//                        @"CIConvolution9Vertical",
//                        @"CILinearToSRGBToneCurve",
                        @"Saturation",
                        @"Vignette",
                        @"Vintage",
                        @"ToneCurve",
                        @"铬黄",
                        @"褪色",
                        @"怀旧",
                        @"单色",
                        @"黑白",
                        @"冲印",
                        @"色调",
                        @"岁月",
                        @"伽玛曲线",
                        @"外亮度",];
        
        [self initFilter];
    }
    return self;
}


- (void)initFilter
{
    
    NSInteger len = [filterArray count];
    NSInteger row = (len%3==0)?(len/3) : (len/3 +1);
    NSInteger col = 3;
    NSInteger w = (self.frame.size.width - 20)/3;
    _scollView.contentSize = CGSizeMake(w*row, self.frame.size.height);
    
    for (NSInteger i=0; i<col; i++) {
        for (NSInteger j=0; j<row; j++) {
            NSInteger idx = j + i*row;
            if (idx >= len) {
                break;
            }
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            btn.frame = CGRectMake(j * w, i*40 + 30, w, 30);
            btn.tag = idx;
            [btn setTitle:[filterNameArray objectAtIndex:idx] forState:UIControlStateNormal];
            [_scollView addSubview:btn];
            [btn addTarget:self action:@selector(selectFilter:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
}

-(void)selectFilter:(id)sender{
    NSInteger idx = [sender tag];
    if (idx < [filterArray count]) {
        NSString *filter = [filterArray objectAtIndex:idx];
        if (self.FiltersViewSelectBlock) {
            self.FiltersViewSelectBlock(filter);
        }
    }
    
    [self removeFromSuperview];
}

@end
